<template>
  <div class="sign">
    <div class="mask">
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="img">
            <img
              src="https://hooray.gitee.io/fantastic-admin-pro-example/assets/login-banner-a565fac8.png"
            />
          </div>
        </el-col>
        <el-col :span="12">
          <div class="sign_right">
            <h3>
              欢迎注册 🤕
              <span>Welcome Sign 🤕 </span>
            </h3>
            <el-form ref="form" :rules="rules" :model="form">
              <el-form-item prop="usernmae" label-width="70px">
                <b> 用户名 </b>
                <el-input
                  maxlength="5"
                  placeholder="请输入username"
                  show-word-limit
                  v-model="form.username"
                  :prefix-icon="User"
                >
                  <template #prefix>
                    <el-icon class="el-input__icon"><User /></el-icon>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item label="" prop="code" label-width="70px">
                <b> 验证码 </b>

                <el-input
                  v-model="form.code"
                  placeholder="请输入验证码"
                  :prefix-icon="Ship"
                >
                  <template #prefix>
                    <!-- 前置插槽 -->
                    <!-- <el-icon class="el-input__icon"><Ship /></el-icon> -->
                  </template>
                  <template #append>
                    <el-button
                      @click="getcode"
                      :disabled="time === 0 ? true : false"
                      >获取验证码 {{ time }}</el-button
                    >
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item label="" label-width="70px" prop="password">
                <b> 密码 </b>
                <el-input
                  v-model="form.password"
                  placeholder="请输入密码"
                  :prefix-icon="Ship"
                >
                  <template #prefix>
                    <el-icon class="el-input__icon"><Ship /></el-icon>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item class="bot">
                <el-button type="primary" @click="onSubmit">sign</el-button>
                <el-button @click="resetForm('form')">Reset</el-button>
              </el-form-item>
              <h5>
                已经注册 ?<a
                  :style="{ color: 'salmon', fontSize: '16px' }"
                  @click="this.$router.push('/login')"
                >
                  登录
                </a>
              </h5>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template> 
 <script>
import { HttpCode, HttpSign } from "api";
import { ElMessage } from "element-plus";

export default {
  data() {
    return {
      time: 3,
      form: {
        code: "",
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "please enter a User Name ",
            trigger: "blur",
          },
          {
            min: 3,
            max: 5,
            message: "The length is 3 to 5 ",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "please enter the password ",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async onSubmit() {
      if (this.form.username === "" && this.form.password === "") {
        ElMessage({
          message: "Please enter the content ! ! !",
          type: "warning",
        });
      } else {
        let res = await HttpSign({ ...this.form });
        ElMessage({
          message: res.data.msg + "!",
          type: res.data.code === 200 ? "success" : "error",
        });
      }
    },
    // 重置数据
    resetForm(form) {
      this.$refs[form].resetFields();
    },
    getcode() {
      // 发出验证码
      const get = setInterval(async () => {
        if (this.time === 0) {
          // 需要清空 否则会重复执行
          const res = await HttpCode();
          ElMessage({
            message: res.data.msg,
            type: "success",
            duration: 2000,
          });
          this.time=3 
          //  停止循环
          clearInterval(get);
          return;
        } else {
          this.time--;
        }
      }, 1000);
    },
  },
};
</script>
 <style scoped lang='scss'>
a {
  text-decoration: none;
  color: white;
  cursor: pointer;
}
.sign {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0%;
  left: 0px;
  // background-image: url("../../assets/img/sign.jpg");
  background-repeat: repeat-x;
  transition: 2s;
  background-size: 100% 100%;
}
h3 {
  line-height: 100px;
  font-size: 30px; 
  span {
    font-size: 15px;
    margin-top: -50px;
    display: block;
  }
}

.mask {
  .el-input {
    height: 40px;
    width: 280px;
  }
  b {
    width: 60px;
    float: left;
    font-size: 18px;
  }
  width: 900px;
  height: 500px;
  position: fixed;
  background-color: white;
  top: 50%;
  // box-shadow: 0px -2px 40px gray ;
  border: solid 1px #ccc;
  left: 50%;
  overflow: hidden;
  border-radius: 20px;
  transform: translate(-50%, -50%);
}
.img {
  width: 100%;
  height: 500px;
  background-color: #eee ;

  img {
    margin: 10px;
    margin-left: -30px;
    width: 120%;
    height: 90%;
  }
}
.sign_right {
  padding: 10px;
}
.bot {
  margin-top: 30px;
  margin-left: 130px;
}

.login_right a {
  float: right;
  margin-right: 10px;
  font-size: 13px;
}
h5 {
  text-align: center; 
}
h6 {
  float: right;
  font-size: 10px;
}
</style>